package callback;

import java.io.IOException;
import java.sql.SQLException;

import model.Time;

import java.text.ParseException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import model.DiscussMessage;
import model.GroupMessage;
import model.GroupUser;
import model.Message;
import AutoResponse.Tuling;
import client.SmartQQClient;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class MyCallBack implements MessageCallback {

	@Override
	public void onMessage(Message message, SmartQQClient client)
			throws IOException {
		// TODO Auto-generated method stub
		System.out.println(Time.getStringDateLong() + " 收到私人消息:"
				+ message.getContent());
		// 去除前后空白符
		String con = replaceBlank(message.getContent());

		// 点歌
		if (con.startsWith("点歌")) {

			String song = message.getContent().replaceFirst("点歌", "");
			song = replaceBlank(song);
			// 歌名为空白，返回
			if (song.equals("")) {
				client.sendMessageToFriend(message.getUserId(), "点歌记得加上歌名哦");
				System.out
						.println(Time.getStringDateLong() + " 收到点歌消息点歌但歌名为空白");
				return;
			}
			song = song.replaceAll("\\s", "%20");
			JSONObject jsonObject = SmartQQClient.getSong(song);

			// 没有找到该歌曲
			if (jsonObject.getString("msg") != null
					&& jsonObject.getString("msg").contains("没有找到")) {
				client.sendMessageToFriend(message.getUserId(),
						"抱歉，小黄鸡没有找到该歌曲哦");
				System.out.println(Time.getStringDateLong() + " 发送私人消息："
						+ "抱歉，小黄鸡没有找到该歌曲哦");
				return;
			}
			String name = jsonObject.getString("name");
			String art = jsonObject.getString("art");
			String url = jsonObject.getString("musicurl");
			String res = "歌曲名：" + name + "\n" + "演唱者：" + art + "\n" + "地址："
					+ url;
			client.sendMessageToFriend(message.getUserId(), res);
			System.out.println(Time.getStringDateLong() + " 发送私人消息：" + res);
			return;
		}

		String answer = Tuling.autoRespon(message.getContent(),
				message.getUserId());
		JSONObject responseJson = JSON.parseObject(answer);
		int code = responseJson.getIntValue("code");
		String text = responseJson.getString("text");
		switch (code) {
		case 100000:
			client.sendMessageToFriend(message.getUserId(), text);
			System.out.println(Time.getStringDateLong() + " 发送私人消息：" + text);
			break;
		case 200000:
			StringBuilder sb = new StringBuilder();
			sb.append(text + "\n");
			String url = responseJson.getString("url");
			sb.append(url);
			client.sendMessageToFriend(message.getUserId(), sb.toString());
			System.out.println(Time.getStringDateLong() + " 发送私人消息："
					+ sb.toString());
			break;
		case 302000:
			StringBuilder sb2 = new StringBuilder();
			sb2.append(text + "\n");
			JSONArray arr = responseJson.getJSONArray("list");
			int size = arr.size();
			int index = (int) (Math.random() * size);
			JSONObject selectNews = arr.getJSONObject(index);
			sb2.append("标题：" + selectNews.getString("article") + "\n");
			sb2.append("来源：" + selectNews.getString("source") + "\n");
			sb2.append("地址：" + selectNews.getString("detailurl") + "\n");
			client.sendMessageToFriend(message.getUserId(), sb2.toString());
			System.out.println(Time.getStringDateLong() + " 发送私人消息："
					+ sb2.toString());
			break;
		case 308000:
			StringBuilder sb3 = new StringBuilder();
			sb3.append(text + "\n");
			JSONArray arr3 = responseJson.getJSONArray("list");
			int randomIndex = (int) (Math.random() * arr3.size());
			JSONObject selectMeals = arr3.getJSONObject(randomIndex);
			// 查菜名
			if (text.contains("菜谱信息")) {
				sb3.append("菜名：" + selectMeals.getString("name") + "\n");
				sb3.append("材料：" + selectMeals.getString("info") + "\n");
				sb3.append("具体介绍：" + selectMeals.getString("detailurl") + "\n");
			} else if (text.contains("电影信息")) {// 查电影
				sb3.append("电影名：" + selectMeals.getString("name") + "\n");
				sb3.append("简介：" + selectMeals.getString("info") + "\n");
				sb3.append("详细地址：" + selectMeals.getString("detailurl") + "\n");
			}
			client.sendMessageToFriend(message.getUserId(), sb3.toString());
			System.out.println(Time.getStringDateLong() + " 发送私人消息："
					+ sb3.toString());
			break;
		default:
			System.out.println(Time.getStringDateLong() + " 异常码：" + code
					+ "，无法识别");
			client.sendMessageToFriend(message.getUserId(), "小黄鸡暂时听不懂你在说什么哦");
		}

	}

	@SuppressWarnings("finally")
	@Override
	public void onGroupMessage(GroupMessage message, SmartQQClient client)
			throws IOException {

		System.out.println(Time.getStringDateLong() + " 收到群消息:"
				+ message.getContent());
		// 去除前后空白符
		String con = replaceBlank(message.getContent());
		// 没有@小黄鸡,不进行回复
		if (!con.startsWith("@小黄鸡")) {
			System.out.println(Time.getStringDateLong() + " 没有@小黄鸡，不进行回复");
			return;
		}
		if (con.equals("@小黄鸡")) {
			client.sendMessageToGroup(message.getGroupId(), "找我干嘛啊");
			System.out.println(Time.getStringDateLong() + " 发送群消息：找我干嘛啊");
			return;
		}
		con = replaceBlank(con.replaceFirst("@小黄鸡", ""));
		// 点歌
		if (con.startsWith("点歌")) {
			String song = con.replaceFirst("点歌", "");
			song = replaceBlank(song);
			// 歌名为空白，返回
			if (song.equals("")) {
				client.sendMessageToGroup(message.getGroupId(), "点歌记得加上歌名哦");
				System.out
						.println(Time.getStringDateLong() + " 收到点歌消息点歌但歌名为空白");
				return;
			}
			song = song.replaceAll("\\s", "%20");
			JSONObject jsonObject = SmartQQClient.getSong(song);
			// 没有找到该歌曲
			if (jsonObject.getString("msg") != null
					&& jsonObject.getString("msg").contains("没有找到")) {
				client.sendMessageToGroup(message.getGroupId(),
						"抱歉，小黄鸡没有找到该歌曲哦");
				System.out.println(Time.getStringDateLong() + " 发送群消息："
						+ "抱歉，小黄鸡没有找到该歌曲哦");
				return;
			}
			String name = jsonObject.getString("name");
			String art = jsonObject.getString("art");
			String url = jsonObject.getString("musicurl");
			String res = "歌曲名：" + name + "\n" + "演唱者：" + art + "\n" + "地址："
					+ url;
			client.sendMessageToGroup(message.getGroupId(), res);
			System.out.println(Time.getStringDateLong() + " 发送群消息：" + res);
			return;
		}

		/*
		 * if(con.equals("签到")){ try { client.signIn(client, message); } catch
		 * (SQLException e) { // TODO Auto-generated catch block
		 * e.printStackTrace(); } catch (ParseException e) { // TODO
		 * Auto-generated catch block e.printStackTrace(); }finally{ return; } }
		 * if(con.equals("查询等级")){ try { client.searchLevel(client, message); }
		 * catch (SQLException e) { // TODO Auto-generated catch block
		 * e.printStackTrace(); } catch (ParseException e) { // TODO
		 * Auto-generated catch block e.printStackTrace(); }finally{ return; } }
		 */

		String answer = Tuling.autoRespon(con, message.getUserId());
		JSONObject responseJson = JSON.parseObject(answer);
		int code = responseJson.getIntValue("code");
		String text = responseJson.getString("text");
		switch (code) {
		case 100000:
			client.sendMessageToGroup(message.getGroupId(), text);
			System.out.println(Time.getStringDateLong() + " 发送群消息：" + text);
			break;
		case 200000:
			StringBuilder sb = new StringBuilder();
			sb.append(text + "\n");
			String url = responseJson.getString("url");
			sb.append(url);
			client.sendMessageToGroup(message.getGroupId(), sb.toString());
			System.out.println(Time.getStringDateLong() + " 发送群消息："
					+ sb.toString());
			break;
		case 302000:
			StringBuilder sb2 = new StringBuilder();
			sb2.append(text + "\n");
			JSONArray arr = responseJson.getJSONArray("list");
			int size = arr.size();
			int index = (int) (Math.random() * size);
			JSONObject selectNews = arr.getJSONObject(index);
			sb2.append("标题：" + selectNews.getString("article") + "\n");
			sb2.append("来源：" + selectNews.getString("source") + "\n");
			sb2.append("地址：" + selectNews.getString("detailurl") + "\n");
			client.sendMessageToGroup(message.getGroupId(), sb2.toString());
			System.out.println(Time.getStringDateLong() + " 发送群消息："
					+ sb2.toString());
			break;
		case 308000:
			StringBuilder sb3 = new StringBuilder();
			sb3.append(text + "\n");
			JSONArray arr3 = responseJson.getJSONArray("list");
			int randomIndex = (int) (Math.random() * arr3.size());
			JSONObject selectMeals = arr3.getJSONObject(randomIndex);
			// 查菜名
			if (text.contains("菜谱信息")) {
				sb3.append("菜名：" + selectMeals.getString("name") + "\n");
				sb3.append("材料：" + selectMeals.getString("info") + "\n");
				sb3.append("具体介绍：" + selectMeals.getString("detailurl") + "\n");
			} else if (text.contains("电影信息")) {// 查电影
				sb3.append("电影名：" + selectMeals.getString("name") + "\n");
				sb3.append("简介：" + selectMeals.getString("info") + "\n");
				sb3.append("详细地址：" + selectMeals.getString("detailurl") + "\n");
			}
			client.sendMessageToGroup(message.getGroupId(), sb3.toString());
			System.out.println(Time.getStringDateLong() + " 发送群消息："
					+ sb3.toString());
			break;
		default:
			System.out.println(Time.getStringDateLong() + " 异常码：" + code
					+ "，无法识别");
			client.sendMessageToGroup(message.getGroupId(), "小黄鸡暂时听不懂你在说什么哦");
		}
	}

	@Override
	public void onDiscussMessage(DiscussMessage message, SmartQQClient client)
			throws IOException {
		// TODO Auto-generated method stub
		System.out.println(Time.getStringDateLong() + " 收到讨论组消息:"
				+ message.getContent());
		// 去除前后空白符
		String con = replaceBlank(message.getContent());
		// 没有@小黄鸡,不进行回复
		if (!con.startsWith("@小黄鸡")) {
			System.out.println(Time.getStringDateLong() + " 没有@小黄鸡，不进行回复");
			return;
		}
		// 仅仅只是@小黄鸡
		if (con.equals("@小黄鸡")) {
			client.sendMessageToDiscuss(message.getDiscussId(), "找我干嘛啊");
			System.out.println(Time.getStringDateLong() + " 发送讨论组消息：找我干嘛啊");
			return;
		}
		con = con.replaceFirst("@小黄鸡", "");
		// 点歌
		if (con.startsWith("点歌")) {
			String song = con.replaceFirst("点歌", "");
			song = replaceBlank(song);
			// 歌名为空白，返回
			if (song.equals("")) {
				client.sendMessageToDiscuss(message.getDiscussId(), "点歌记得加上歌名哦");
				System.out
						.println(Time.getStringDateLong() + " 收到点歌消息点歌但歌名为空白");
				return;
			}
			song = song.replaceAll("\\s", "%20");
			JSONObject jsonObject = SmartQQClient.getSong(song);
			// 没有找到该歌曲
			if (jsonObject.getString("msg") != null
					&& jsonObject.getString("msg").contains("没有找到")) {
				client.sendMessageToDiscuss(message.getDiscussId(),
						"抱歉，小黄鸡没有找到该歌曲哦");
				System.out.println(Time.getStringDateLong() + " 发送讨论组消息："
						+ "抱歉，小黄鸡没有找到该歌曲哦");
				return;
			}
			String name = jsonObject.getString("name");
			String art = jsonObject.getString("art");
			String url = jsonObject.getString("musicurl");
			String res = "歌曲名：" + name + "\n" + "演唱者：" + art + "\n" + "地址："
					+ url;
			;
			client.sendMessageToDiscuss(message.getDiscussId(), res);
			System.out.println(Time.getStringDateLong() + " 发送讨论组消息：" + res);
			return;
		}
		String answer = Tuling.autoRespon(con, message.getUserId());
		JSONObject responseJson = JSON.parseObject(answer);
		int code = responseJson.getIntValue("code");
		String text = responseJson.getString("text");
		switch (code) {
		case 100000:
			client.sendMessageToDiscuss(message.getDiscussId(), text);
			System.out.println(Time.getStringDateLong() + " 发送讨论组消息：" + text);
			break;
		case 200000:
			StringBuilder sb = new StringBuilder();
			sb.append(text + "\n");
			String url = responseJson.getString("url");
			sb.append(url);
			client.sendMessageToDiscuss(message.getDiscussId(), sb.toString());
			System.out.println(Time.getStringDateLong() + " 发送讨论组消息："
					+ sb.toString());
			break;
		case 302000:
			StringBuilder sb2 = new StringBuilder();
			sb2.append(text + "\n");
			JSONArray arr = responseJson.getJSONArray("list");
			int size = arr.size();
			int index = (int) (Math.random() * size);
			JSONObject selectNews = arr.getJSONObject(index);
			sb2.append("标题：" + selectNews.getString("article") + "\n");
			sb2.append("来源：" + selectNews.getString("source") + "\n");
			sb2.append("地址：" + selectNews.getString("detailurl") + "\n");
			client.sendMessageToDiscuss(message.getDiscussId(), sb2.toString());
			System.out.println(Time.getStringDateLong() + " 发送讨论组消息："
					+ sb2.toString());
			break;
		case 308000:
			StringBuilder sb3 = new StringBuilder();
			sb3.append(text + "\n");
			JSONArray arr3 = responseJson.getJSONArray("list");
			int randomIndex = (int) (Math.random() * arr3.size());
			JSONObject selectMeals = arr3.getJSONObject(randomIndex);
			// 查菜名
			if (text.contains("菜谱信息")) {
				sb3.append("菜名：" + selectMeals.getString("name") + "\n");
				sb3.append("材料：" + selectMeals.getString("info") + "\n");
				sb3.append("具体介绍：" + selectMeals.getString("detailurl") + "\n");
			} else if (text.contains("电影信息")) {// 查电影
				sb3.append("电影名：" + selectMeals.getString("name") + "\n");
				sb3.append("简介：" + selectMeals.getString("info") + "\n");
				sb3.append("详细地址：" + selectMeals.getString("detailurl") + "\n");
			}
			client.sendMessageToDiscuss(message.getDiscussId(), sb3.toString());
			System.out.println(Time.getStringDateLong() + " 发送讨论组消息："
					+ sb3.toString());
			break;
		default:
			System.out.println(Time.getStringDateLong() + " 异常码：" + code
					+ "，无法识别");
			client.sendMessageToDiscuss(message.getDiscussId(),
					"小黄鸡暂时听不懂你在说什么哦");
		}
	}

	// 去除字符串首位空白符的方法
	private String replaceBlank(String str) {
		Pattern pt = Pattern.compile("^\\s*|\\s*$");
		Matcher mt = pt.matcher(str);
		str = mt.replaceAll("");
		return str;
	}
}
